package com.kmproject.haifu.mvp.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HFLocationBean;
import com.kmproject.haifu.mvp.ui.activity.HouseRentShowInfoActivity;
import com.kmproject.haifu.mvp.ui.activity.UserSetEditInfoActivity;
import com.kmproject.haifu.mvp.ui.widget.RoundCornerImageView;
import com.library.flowlayout.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class HouseRentingShowListAdapter extends BaseQuickAdapter<HouseRentListShowBean,BaseViewHolder> {

    ImageLoader glideImage;
    private HFLocationBean mbdLocation;
    public HouseRentingShowListAdapter(@Nullable List<HouseRentListShowBean> data) {
        super(R.layout.houserenting_main_content_item, data);

    }

    public void setMbdLocation(HFLocationBean mbdLocation) {
        this.mbdLocation = mbdLocation;
    }

    @Override
    protected void convert(BaseViewHolder helper, HouseRentListShowBean item) {
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
        //判断是否关联了店铺
        if (item.getUserStoreName()!=null&&!TextUtils.isEmpty(item.getUserStoreName())&&item.getUserStoreIsShow()==1)
        {
        //关联了店铺
            helper.<LinearLayout>getView(R.id.houserenting_main_content_item_store_lay).setVisibility(View.VISIBLE);
            helper.<TextView>getView(R.id.houserenting_main_content_item_store_name).setText(item.getUserStoreName());
        }else {
            //未关联店铺
            helper.<LinearLayout>getView(R.id.houserenting_main_content_item_store_lay).setVisibility(View.GONE);
        }
        if (item.getHouseImageList()!=null&&!item.getHouseImageList().isEmpty())
        {
            glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getHouseImageList().get(0).getImageUrl()).imageRadius(5)
                    .imageView(helper.<RoundCornerImageView>getView(R.id.houserenting_main_content_item_img))
                    .build());
            helper.<RoundCornerImageView>getView(R.id.houserenting_main_content_item_img).setmBorderRadius(5);
        }


        String temName = "";
        if (item.getHouseAreaName() != null && !item.getHouseAreaName().isEmpty()) {

            for (int i = 0; i < item.getHouseAreaName().size(); i++) {
                if (i != item.getHouseAreaName().size() - 1) {
                    temName += item.getHouseAreaName().get(i).getName() + ".";
                } else {
                    temName += item.getHouseAreaName().get(i).getName();
                }
            }
        }
        helper.getView(R.id.houseRentingShow_item_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,HouseRentShowInfoActivity.class);
                intent.putExtra("rentId",item.getRentId());
                if (mbdLocation!=null)
                {
                    intent.putExtra("lgt",mbdLocation.getMlocation().getLongitude()+"");
                    intent.putExtra("lat",mbdLocation.getMlocation().getLatitude()+"");
                }else {
                    intent.putExtra("lgt","0");
                    intent.putExtra("lat","0");
                }
                UserBean userBean = DataHelper.getDeviceData(mContext, AppConstant.USER_INFO);//获取用户信息
                if (userBean!=null&&userBean.getUserId()!=null&&userBean.getUserId().equals(item.getUserId()))
                {
                    intent.putExtra("type","userCenterInfo");
                }
                ArmsUtils.startActivity(intent);
            }
        });
        if (!item.getRentType().equals("dz"))
        {
            helper.<TextView>getView(R.id.houserenting_main_content_item_unit).setText("/月");
        } else
        {
            helper.<TextView>getView(R.id.houserenting_main_content_item_unit).setText("/天");
        }
        HouseRentingShowHeZuYqItemAdapter adapter;
        List<HouseRentListShowBean.DetailsBean> detailsBeans=new ArrayList<>();
        if ("jmzz".equals(item.getHouseCategory()))
        {
            String temHouseTitle=item.getRentTypeName()+"."+item.getRoomCountName()+item.getDrawingCountName()+item.getToiletCountName();
            if (item.getRentType().equals("hz"))
            {
                //房屋类型
                temHouseTitle+="."+item.getRentRoomName();
            }
            temHouseTitle+="."+item.getHouseName();

            //居民住宅
            helper.<TextView>getView(R.id.houserenting_main_content_item_txt1).setText(temHouseTitle);
            helper.<TextView>getView(R.id.houserenting_main_content_item_txt2).setText(item.getAcreage()+"m² "+AppUtil.showFloor(item)+" "+temName);
            helper.<TextView>getView(R.id.houserenting_main_content_item_price).setText(item.getFormatMonthRent()+"P");
            if (item.getRentType().equals("zz")||item.getRentType().equals("hz"))
            {
                //整租合租  显示前三个亮点
                if (item.getDetails()!=null&&!item.getDetails().isEmpty())
                {
                    if (item.getDetails().size()>2)
                    {
                        detailsBeans.addAll(item.getDetails().subList(0,3));
                    }else {
                        detailsBeans.addAll(item.getDetails());
                    }
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.VISIBLE);
                }else {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.GONE);
                }

            }else if (item.getRentType().equals("bs"))
            {
                //别墅 别墅显示装修和两个亮点
                if (item.getDecorationTypeName().contains("精")||item.getDecorationTypeName().contains("豪"))
                {
                    detailsBeans.add(new HouseRentListShowBean.DetailsBean(item.getDecorationTypeName()));
                }
                if (item.getDetails()!=null&&!item.getDetails().isEmpty())
                {
                    if (item.getDetails().size()>1)
                    {
                        detailsBeans.addAll(item.getDetails().subList(0,2));
                    }else {
                        detailsBeans.addAll(item.getDetails());
                    }
                }
                if (detailsBeans.isEmpty())
                {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.GONE);
                }else {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.VISIBLE);
                }

            }else {
                //短租 显示房屋类型和两个房屋亮点
                detailsBeans.add(new HouseRentListShowBean.DetailsBean(item.getRentRoomName()));
                if (item.getDetails()!=null&&!item.getDetails().isEmpty())
                {
                    if (item.getDetails().size()>1)
                    {
                        detailsBeans.addAll(item.getDetails().subList(0,2));
                    }else {
                        detailsBeans.addAll(item.getDetails());
                    }
                }
                if (detailsBeans.isEmpty())
                {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.GONE);
                }else {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.VISIBLE);
                }
            //  rentRoomName
            }
            adapter=  new HouseRentingShowHeZuYqItemAdapter(detailsBeans);
            helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setLayoutManager(new GridLayoutManager(mContext, 3));
             if (helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).getItemDecorationCount()==0){
                 helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).addItemDecoration(new SpaceItemDecoration(AppUtil.dp2px(2)),0);
             }
            helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setAdapter(adapter);

            return;
        }
        //商业用地
        helper.<TextView>getView(R.id.houserenting_main_content_item_txt1).setText(item.getHouseName());
        helper.<TextView>getView(R.id.houserenting_main_content_item_txt2).setText(item.getAcreage()+"m² "+temName);
        helper.<TextView>getView(R.id.houserenting_main_content_item_price).setText(String.valueOf(item.getMonthRent().intValue())+"P");
         if ("sp".equals(item.getRentType()))
         {
             //商铺
             if (item.getTransferOrLease().equals("transfer"))
             {
                 detailsBeans.add(new HouseRentListShowBean.DetailsBean("商铺转让"));
             }else {
                 detailsBeans.add(new HouseRentListShowBean.DetailsBean("商铺出租"));
             }
             if (item.getDailyRent().doubleValue()>0)
             {
                 detailsBeans.add(new HouseRentListShowBean.DetailsBean("短租"));
             }
         }else {
             //仓库，写字楼，办公室
             detailsBeans.add(new HouseRentListShowBean.DetailsBean(item.getRentTypeName()));
             if (item.getDailyRent().doubleValue()>0)
             {
                 detailsBeans.add(new HouseRentListShowBean.DetailsBean("短租"));
             }
             if ("bgs".equals(item.getRentType()))
             {
                 //办公室
                 if (item.getIsSeparable()!=null&&"1".equals(item.getIsSeparable()))
                 {
                     //是否可分割
                     detailsBeans.add(new HouseRentListShowBean.DetailsBean("可分割"));
                 }
                 if (item.getIsSeparable()!=null&&"1".equals(item.getIsOfficeAppliances()))
                 {
                     //是否可分割
                     detailsBeans.add(new HouseRentListShowBean.DetailsBean("带办公家具"));
                 }
             }
         }
        helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setLayoutManager(new GridLayoutManager(mContext, 3));
         if (helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).getItemDecorationCount()==0){
             helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).addItemDecoration(new SpaceItemDecoration(AppUtil.dp2px(2)),0);
         }
        helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setAdapter(new HouseRentingShowHeZuYqItemAdapter(item.getDetails()));
    }
}
